package com.five.community.controller;

import com.five.community.bean.Village;

import com.five.community.bean.vo.ActivityVo;
import com.five.community.contant.AppContants;
import com.five.community.service.VillageService;
import com.five.community.util.FileUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/village")
public class VillageController {
    @Autowired
    private VillageService villageService;
//    小区一览页面
    @RequestMapping("/listUi")
    public String listUi(ModelMap map,Integer pageNum){
        if (pageNum == null) {
            pageNum = 1;
        }
        try {
            PageInfo<Village> pageInfo = villageService.getPageInfo(pageNum, AppContants.pageSize);
            map.addAttribute("pageInfo",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "villagelist";
    }
    //小区名字查询
    @RequestMapping("/selectUi")
    public String selectUi(ModelMap map,String name,Integer pageNum){
        if (name==""){
            return "redirect:/village/listUi";
        }
        else {
            try {
                if (pageNum == null) {
                    pageNum = 1;
                }
                PageHelper.startPage(pageNum,AppContants.pageSize);
                List<Village> villages =  villageService.selectByName(name);
                PageInfo<Village> pageInfo = new PageInfo<>(villages);
                map.addAttribute("pageInfo", pageInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "villagelist";
    }
    //小区添加页面
    @RequestMapping("addUi")
    private String addUi(){
        return "village_add";
    }
    //小区页面添加操作
    @RequestMapping("/add")
    public String add(Village village, MultipartFile headerFile){
        System.out.println("headerFile = " + headerFile);
        String fileUpload="";
        try {
            fileUpload = FileUtils.fileUpload(headerFile);
            village.setThumbnail(fileUpload);
        } catch (IOException e) {
            e.printStackTrace();
        }

//        //获得上传文件原始名字
//        String originalFilename = headerFile.getOriginalFilename();
//        //获得文件后缀名
//        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//        //去掉文件名之间的"-"
//        String stringname = FileUtils.getUUIDFilename();
//        //文件名加后缀名
//        stringname = substring + substring;
//        //设置保存路径
//        File file=new File("D:\\images\\" + stringname);
//
//        try {
//            //上传图片到路径
//            headerFile.transferTo(file);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        //设置数据
        village.setCreateDate(new Date());

        //调用服务
        try {
            villageService.insert(village);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/village/addUi";
    }
    //小区编辑修改画面
    @RequestMapping("/editUi")
    public String editUi(long id,ModelMap map){
        try {
            Village village = villageService.selectById(id);
            map.addAttribute("village",village);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "village_edit";
    }
    //小区编辑
    @RequestMapping("/edit")
    public String edit(Village village,MultipartFile headerFile,String editCreateDate){
        String originalFilename = headerFile.getOriginalFilename();
        if(originalFilename!=""){
            try {
                String fileUpload = FileUtils.fileUpload(headerFile);
                village.setThumbnail(fileUpload);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            villageService.update(village);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "village_edit";
    }
    //删除小区
    @RequestMapping("/delete")
    public String delete(long id){

        try {
            villageService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/village/listUi";
    }

}
